import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { HttpService } from '../../shared/service/http.service';
import { RecommendModalComponent } from '../../coms/recommend-modal/recommend-modal.component';
import { ModalController, AlertController, IonContent } from '@ionic/angular';
import { ModalFromCenterLeave, ModalFromCenterEnter } from 'src/app/utils/transitions';


@Component({
  selector: 'app-tiptitle',
  templateUrl: './tiptitle.component.html',
  styleUrls: ['./tiptitle.component.scss'],
})
export class TiptitleComponent implements OnInit {
  @Input() tagData: any;
  listdata: any;
  cssStr = {}; // 传入样式
  appTypeCustom: any;
  constructor(private appGlobalService: AppGlobalService, private http: HttpService, private modalController: ModalController) { }

  ngOnInit() {
    const bodyStyles = window.getComputedStyle(document.body);
    this.appTypeCustom = bodyStyles.getPropertyValue('--appTypeCustom').trim().replace('"', '').replace('"', '');
    this.listdata = this.tagData.data;
    if (this.tagData.data.cssStyle) {
      const cssStyles = this.tagData.data.cssStyle.split(";");
      cssStyles.map(v => {
        const cssStrs = v.split(":");
        this.cssStr[cssStrs[0]] = cssStrs[1];
      });
    }
  }
  // 弹出框
  popopen(obj) {
    /*  console.log(obj)
     if (obj) {
       const pageParams = {
         pageId: obj.target,
         params: obj.params,
         sessionId: localStorage.getItem('sessionId')
       };
       console.log(pageParams);
       this.appGlobalService.goToForward(pageParams);
     } */
    this.selectItem(obj);
  }
  selectItem(item) {
    // console.log(item);
    if (item.type === 'modal') {
      this.openRankingModal("recommend-modal", item);
    }
  }
  async openRankingModal(className, item) {
    const params = {
      sessionId: localStorage.getItem("sessionId"),
      pageId: item.target,
      params: item.params
    };
    this.http.post('application/page', params, async res => {
      const tagData = JSON.parse(res.script).views[0].tags[0].data;
      // "ranking-modal"
      const modal = await this.modalController.create({
        showBackdrop: true,
        cssClass: className,
        component: RecommendModalComponent,
        componentProps: { tagData },
        leaveAnimation: ModalFromCenterLeave,
        enterAnimation: ModalFromCenterEnter
      });
      await modal.present();
      // 监听销毁的事件
      const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    });
  }
  getNgStyle(str) {
    try {
      return JSON.parse(str);
    } catch (error) {
      return {};
    }
  }
}
